
var legendSeriesFormat = `<span style="width:122px;background:#121A1C;opacity: 0.95;display: inline-flex"><span style="flex:1;text-align: right;font-family: PingFangSC-Regular;flex-grow: 1;font-size: 10px;color: {0};">{1} :</span><span style="flex:1;background-color:#{2};opacity: 0.8;font-family: PingFangSC-Regular;font-size: 10px;color: {3};text-align: right;flex-grow: 1;">{4}</span></span>`;
function legendTxtFormatter(nameColor, seriesName, valBgColor, valFgColor, val) {
 

    return legendSeriesFormat.format(nameColor, seriesName, valBgColor, valFgColor, getLegendValue( val ) );
}

String.prototype.format = String.prototype.f = function() {
    var s = this,
      i = arguments.length;
  
    while (i--) {
      s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
    }
    return s;
  };

function getLegendValue( value ) {
    var valueString = "";
    if ( isValued( value ) == false ) {
      return "";
    }
  
    if ( typeof value == "string") {
      return value;
    }
    if ( Array.isArray( value ) == false ) {
       valueString = value.toFixed( 3 );
       return valueString;
    }  
    var index = 0;
    var ohlc = value.length == 4;
    value.forEach( function( member ) {
      if ( valueString.length > 0 ) {
        valueString = valueString + ",";
      }
      try{
        valueString = valueString + member.toFixed( 3 );
      }catch(e){
        console.error("ERROR-->",member);
      }
    });
  
    return "(" + valueString + ")";
}

function labelFormatter() {
    function errorMessageResolver( series, lastValue ) {
        if ( lastValue == null && series.userOptions.errorMessage ) {
          return series.userOptions.errorMessage;
        }
        return lastValue;
      }

    function extractValue( value ) {
        if ( Array.isArray( value ) ) {
          return value;
        }

        if ( typeof value == "number") {
          return value;
        }

        if ( isValued( value ) == false ) {
          return null;
        }

        if ( isValued( value.open ) ) {
          return [ value.open, value.high, value.low, value.close ];
        }
        if ( isValued( value.y ) ) {
          return value.y;
        }

        return null;
    }
    var lastValue = null;
    //check for hovering points
    var hoverPoints = this.chart.legend.getHoverPoints();
    var currentPoint = this.point;
    var color1 = this.color;
    var valueBackground = this.backgroundColor;
    var valueForeground = this.color;
    if ( hoverPoints && hoverPoints.length > 0 ) {
    var match = null;
    var linkedSeriesName = null;
    if ( this.linkedSeries.length > 0 ) {
        linkedSeriesName = this.linkedSeries[ 0 ].name;
    }

    for ( var index = 0; index < hoverPoints.length; index++ ) {
        var hoverPoint = hoverPoints[ index ];
        if ( hoverPoint.series.name == this.name || hoverPoint.series.name == linkedSeriesName ) {
        match = hoverPoint;
        break;
        }
    }
    if ( match != null ) {
        currentPoint = match;
    }
    }

    if ( isValued( currentPoint )  ) {
    lastValue = extractValue( currentPoint );
    }
    if ( lastValue == null && this.yData.length > 0 ) {
    //make current series color the background color and find the inverse color for the foreground
    valueBackground = this.color;
    lastValue = extractValue( this.yData[ this.yData.length - 1 ] );
    }

    //we are in a historical candlestick situation where we reach back into the linked series for its value
    if ( lastValue == null && this.linkedSeries.length > 0 ) {
    //go to historical series for value
    var linkedSeries = this.linkedSeries[ 0 ];
    if ( linkedSeries.yData.length > 0 ) {
        lastValue = extractValue( linkedSeries.yData[ linkedSeries.yData.length - 1 ]  );
    }
    }

    return legendTxtFormatter("#FFFFFF", this.name, valueBackground, "#FFFFFF", errorMessageResolver( this, lastValue ) )
}